package com.nursinghome.admin.controller;

import com.nursinghome.bean.RespBean;
import com.nursinghome.bean.Service;
import com.nursinghome.common.service.ServiceService;
import com.nursinghome.exception.NurisingHomeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/admin/service")
public class ServiceController {
    @Autowired
    private ServiceService serviceService;
    @GetMapping
    public RespBean selectAll(String pageNum, String pageSize, String prop ,String order, Service service) {

        return RespBean.ok("",serviceService.selectAll(pageNum, pageSize, prop, order, service));
    }
    //新增商品
    @PostMapping
    public RespBean insert(@RequestBody Service service) throws NurisingHomeException {
        if (serviceService.insert(service)) {
            return RespBean.ok("新增成功");
        }
        return RespBean.error("新增失败");
    }
    //修改商品
    @PutMapping
    public RespBean update(@RequestBody Service service) throws NurisingHomeException {
        if (serviceService.update(service)) {
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败");
    }
    //删除商品
    @DeleteMapping("/{id}")
    public RespBean delete(@PathVariable("id") Integer id) throws NurisingHomeException {
        if (serviceService.delete(id)) {
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }
    //回显商品数据
    @GetMapping("/{id}")
    public RespBean selectById(@PathVariable("id") Integer id) throws NurisingHomeException {
        Service service = serviceService.selectById(id);
        return RespBean.ok("",service);
    }
}
